import 'package:flutter/material.dart';
import 'dart:convert' as convert;
import 'package:http/http.dart' as http;
import 'package:dio/dio.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => HomePageState();
}

class HomePageState extends State {
  List _listData = [];
  List _dioListData = [];
  String loading = '点击get获取内容';

  @override
  Widget build(BuildContext context) {
    return ListView(
      shrinkWrap: true,
      children: [
//        Column(
//            children: _listData.length > 0
//                ? _listData.map((value) {
//                    return Text('${value['title']}');
//                  }).toList()
//                : [Text('$loading')]),
//
//        Expanded(
//            child: ListView(
//                children: [
//                  _listData.length > 0
//                      ? ListView.builder(
//                    shrinkWrap:true,
//                    itemCount: _listData.length,
//                    itemBuilder: (context, index) {
//                      return Text('${_listData[index]["title"]}');
//                    },
//                  )
//                      : Text('$loading')
//                ]
//            ),
//        ),


//        Expanded(
//          child: Column(
//              children: [
//                _dioListData.length>0 ? ListView.builder(
//                  shrinkWrap: true,
//                  itemCount: _dioListData.length,
//                  itemBuilder: (context,index){
//                    return Text('${_dioListData[index]['title']}');
//                  },
//                ) : Text(loading)
//              ]
//          ),
//        ),

//        Expanded(
//            child: ListView(
//                shrinkWrap: true,
//                children: [
//                  _dioListData.length>0 ? ListView.builder(
//                    shrinkWrap: true,
//                    itemCount: _dioListData.length,
//                    itemBuilder: (context,index){
//                      return Text('${_dioListData[index]['title']}');
//                    },
//                  ) : Text(loading)
//                ]
//            ),
//        ),

        Column(
            children: [
              Expanded(
                child: Column(
                  children: [
                    _dioListData.length>0 ? ListView.builder(
                      shrinkWrap: true,
                      itemCount: _dioListData.length,
                      itemBuilder: (context,index){
                        return Text('${_dioListData[index]['title']}');
                      },
                    ) : Text(loading)
                  ],
                )
              )
            ]
        ),

        SizedBox(height: 50),

        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            RaisedButton(
              textColor: Colors.white,
              color: Colors.blue,
              child: Text('get请求'),
              onPressed: _getData,
            ),
          ],
        ),
        SizedBox(height: 50),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            RaisedButton(
              textColor: Colors.white,
              color: Colors.blue,
              child: Text('post请求'),
              onPressed: () {},
            ),
          ],
        ),
        SizedBox(height: 50),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            RaisedButton(
              textColor: Colors.white,
              color: Colors.blue,
              child: Text('Dio方式get请求'),
              onPressed: _dioGetData,
            ),
          ],
        ),
      ],
    );
  }

  _getData() async {
    this.setState(() {
      loading = '正在加载中...';
    });
    var url = 'http://a.itying.com/api/productlist';
    var response = await http.get(url);

    if (response.statusCode == 200) {
      var jsonResponse = convert.jsonDecode(response.body);
      this.setState(() {
        _listData = jsonResponse['result'];
      });
    } else {
      print('Request failed with status: ${response.statusCode}.');
    }
  }

  _dioGetData () async {
    this.setState(() {
      loading = '正在获取数据...';
    });

    var dio = Dio();
    var url = 'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1';

    this.setState(() {
      loading = '正在加载中...';
    });
    Response response = await dio.get(url);

    List res = convert.json.decode(response.data)['result'];

    this.setState(() {
      _dioListData = res;
    });
  }


}
